package Demo1299.Demo15;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        List<Animal> list = list();
        for (Animal animal : list) {
            System.out.println(animal);
        }
    }

    private static List<Animal> list() throws Exception {
        List<Animal> animals = new ArrayList<>();
        List<String> list = FileUtils.readLines(new File("C:\\Daymate\\src\\Demo1299\\Demo15\\dataAnimal.txt"));
        for (String s : list) {
            String[] split = s.split(" ");
            Class<?> aClass = Class.forName(split[0]);
            Object c = aClass.getDeclaredConstructor().newInstance();
            try {
                Cat cat = (Cat) c;
                Object Cat = aClass.getDeclaredConstructor(String.class, String.class, String.class).newInstance(split[1].split("=")[1], split[2].split("=")[1], split[3].split("=")[1]);
                animals.add((Animal) Cat);
            } catch (Exception e) {
                int i = Integer.parseInt(split[3].split("=")[1]);
                Object Dog = aClass.getDeclaredConstructor(String.class, String.class, int.class).newInstance(split[1].split("=")[1], split[2].split("=")[1],i);
                animals.add((Animal) Dog);
            }
        }
        return animals;
    }
}
